package com.dmap.community.service.community.impl;

import com.dmap.community.bo.theme.ShowThemeBo;
import com.dmap.community.cache.UserCacheDao;
import com.dmap.community.controller.app.param.ThemeParam;
import com.dmap.community.dao.ThemeMapper;
import com.dmap.community.po.Theme;
import com.dmap.community.po.ThemeExample;
import com.dmap.community.po.cache.UserCachePo;
import com.dmap.community.service.community.CollectService;
import com.dmap.community.service.community.ReplyService;
import com.dmap.community.service.community.ThemeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * \* User: silent
 * \* Date: 14/11/17 Time: 10:53
 * \* Description:
 * \
 */
@Service
public class ThemeServiceImpl implements ThemeService {
    @Resource
    private ThemeMapper themeMapper;

    @Resource
    private CollectService collectService;
    @Resource
    private ReplyService replyService;
    @Resource
    private UserCacheDao userCacheDao;

    @Override
    public int insertTheme(ThemeParam param) {
        Theme theme = new Theme();
        theme.setComment(param.getComment());
        theme.setImageUrl(param.getImageUrl());
        theme.setCreateTime(System.currentTimeMillis() / 1000);
        theme.setType(param.getType());
        theme.setUserId(theme.getUserId());
        return themeMapper.insertSelective(theme);
    }

    /**
     * 删除主题
     * the first step delete reply of theme and delete collect of theme
     * the second step delete theme
     */
    @Transactional
    @Override
    public int deleteTheme(long themeId) {
        int success = 0;
        success = success + collectService.deleteCollectByThemeId(themeId, 0);
        success = success + replyService.deleteReplyTheme(themeId);
        success = success + themeMapper.deleteByPrimaryKey(themeId);
        return success;
    }

    @Override
    public int countPostThemeNumber(long userId) {
        ThemeExample themeExample = new ThemeExample();
        themeExample.createCriteria().andUserIdEqualTo(userId);
        return themeMapper.countByExample(themeExample);
    }

    @Override
    public List<ShowThemeBo> showThemeListByType(int type, int pageIndex, int pageSize) {
        List<ShowThemeBo> showThemeBoList = new ArrayList<>();
        ThemeExample themeExample = new ThemeExample();
        themeExample.setLimitStart((pageIndex - 1) * pageSize);
        themeExample.setLimitEnd(pageSize);
        themeExample.setOrderByClause("desc create_time");
        themeExample.createCriteria().andTypeEqualTo(type);
        List<Theme> themeList = themeMapper.selectByExample(themeExample);
        packThemeData(showThemeBoList, themeList);
        return showThemeBoList;
    }

    @Override
    public List<ShowThemeBo> showThemeListByUserId(long userId, int pageIndex, int pageSize) {
        List<ShowThemeBo> showThemeBoList = new ArrayList<>();
        ThemeExample themeExample = new ThemeExample();
        themeExample.setLimitStart((pageIndex - 1) * pageSize);
        themeExample.setLimitEnd(pageSize);
        themeExample.setOrderByClause("desc create_time");
        themeExample.createCriteria().andUserIdEqualTo(userId);
        List<Theme> themeList = themeMapper.selectByExample(themeExample);
        packThemeData(showThemeBoList, themeList);
        return showThemeBoList;
    }

    @Override
    public ShowThemeBo selectThemeByThemeId(long themeId) {
        ShowThemeBo showThemeBo = new ShowThemeBo();
        Theme theme = themeMapper.selectByPrimaryKey(themeId);
        if (theme != null) {
            showThemeBo = new ShowThemeBo(theme);
            UserCachePo userCachePo = userCacheDao.selectUserCacheInformation(theme.getUserId());
            showThemeBo.setNikName(userCachePo.getNickname());
            showThemeBo.setPortraitUrl(userCachePo.getPortraitUrl());
            showThemeBo.setCollectCount(collectService.countCollectThemeNumbersByThemeId(theme.getId()));
            showThemeBo.setReplyCount(replyService.countReplyThemeNumber(theme.getId()));
        }
        return showThemeBo;
    }

    @Override
    public List<ShowThemeBo> selectThemeListByThemeIds(List<Long> themeIds) {
        List<ShowThemeBo> showThemeBoList = new ArrayList<>();
        ThemeExample themeExample = new ThemeExample();
        themeExample.createCriteria().andIdIn(themeIds);
        List<Theme> themeList = themeMapper.selectByExample(themeExample);
        packThemeData(showThemeBoList, themeList);
        return showThemeBoList;
    }

    private void packThemeData(List<ShowThemeBo> showThemeBoList, List<Theme> themeList) {
        themeList.forEach(theme -> {
            ShowThemeBo showThemeBo = new ShowThemeBo(theme);
            UserCachePo userCachePo = userCacheDao.selectUserCacheInformation(theme.getUserId());
            showThemeBo.setPortraitUrl(userCachePo.getPortraitUrl());
            showThemeBo.setNikName(userCachePo.getNickname());
            showThemeBo.setCollectCount(collectService.countCollectThemeNumbersByThemeId(theme.getId()));
            showThemeBo.setReplyCount(replyService.countReplyThemeNumber(theme.getId()));
            showThemeBoList.add(showThemeBo);
        });

    }


}
